//
//  UsersViewController.m
//  monkey
//
//  Created by lidi on 15/8/4.
//  Copyright (c) 2015年 colin. All rights reserved.
//

#import "UsersViewController.h"
#import "UsersTableViewCell.h"
#import "GMDCircleLoader.h"
#import "UIImageView+WebCache.h"
//#import "YiRefreshFooter.h"
//#import "YiRefreshHeader.h"

@interface UsersViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
//    YiRefreshHeader *refreshHeader;
//    YiRefreshFooter *refreshFooter;
}
@property (weak, nonatomic) IBOutlet UITableView *infoTableview;
@property (strong,nonatomic) NSMutableArray *usersDatas;

@end

@implementation UsersViewController
- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    //[GMDCircleLoader setOnView:self.view withTitle:@"Loading..." animated:YES];
    //[self performSelector:@selector(stopCircleLoader) withObject:nil afterDelay:5];
    
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
                                                                    defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.github.com/users?since=135"]];
    
    request.HTTPMethod = @"GET";
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSError *jsonError;
        
        id resData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
        //NSLog(@"%@",resData);
        self.usersDatas = [NSMutableArray array];
        [self.usersDatas addObjectsFromArray:resData];
        [self.infoTableview reloadData];
        
    }];
    
    [task resume];
    
//    NSError *error;
//    //加载一个NSURL对象
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.github.com/users?since=135"]];
//    //将请求的url数据放到NSData对象中
//    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//    //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典或者数组中
//    NSArray *array1 = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
//    self.usersDatas = [NSMutableArray array];//设置usersDatas为一个可变数组
//    [self.usersDatas addObjectsFromArray:array1];//把array1数组中的元素添加到usersDatas中去
//    [self.infoTableview reloadData];//重新加载此tableview的数据

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.usersDatas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UsersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UsersTableViewCell"];
    
    NSDictionary *userdata = [self.usersDatas objectAtIndex:indexPath.row];
    
    cell.labelName.text = [userdata objectForKey:@"login"];
    cell.lableId.text = [NSString stringWithFormat:@"%@",[userdata objectForKey:@"id"]];
//   cell.imageIcon.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[userdata objectForKey:@"avatar_url"]]]];
    [cell.imageIcon sd_setImageWithURL:[NSURL URLWithString:[userdata objectForKey:@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //NSDictionary *listenData = [self.listenDatas objectAtIndex:indexPath.row];
    NSDictionary *userdata = [self.usersDatas objectAtIndex:indexPath.row];
    
    //NSLog(@"%@",[listenData objectForKey:@"Title"]);
    
}

//- (void)stopCircleLoader {
//    [GMDCircleLoader hideFromView:self.view animated:YES];
//}

@end
